package com.webserver.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * 请求对象
 * 该类的每一个实例用于表示HTTP协议规定的一个请求内容
 * 每个请求由3部分构成
 * 请求行，消息头，消息正文
 */
public class HttpRequest {

    //请求行相关信息
    private String method;      //请求方式
    private String uri;         //抽象路径
    private String protocol;    //版本协议

    //消息头相关信息
    private Map<String ,String> headers =new HashMap<>();

    //消息正文相关信息

    private Socket socket;

    public HttpRequest(Socket socket) throws IOException {
        this.socket = socket;
        //1.1读取请求行
        parseRequestLine();
        //1.2解析消息头
        parseHanders();
        //1.3解析消息正文
        parseContent();
    }

    //1.1读取请求行
    private void parseRequestLine() throws IOException {
        String line = readline();
        System.out.println(line);



        String[] data = line.split("\\s");
        method = data[0];
        uri = data[1];                    //这里后期会出现数组越界异常，这是用于空请求造成的，后期解决
        protocol = data[2];
        System.out.println("method = " + method);           // GET
        System.out.println("uri = " + uri);                 // /index.html
        System.out.println("protocol = " + protocol);       // HTTP/1.1

           /* //自己写的
            method=line.substring(0,line.indexOf('/'));
            System.out.println("method = " + method);           // GET
            uri=line.substring(line.indexOf('/'),line.indexOf("HTTP"));
            System.out.println("uri = " + uri);                 // /index.html
            protocol=line.substring(line.indexOf("HTTP"),line.length());
            System.out.println("protocol = " + protocol);       // HTTP/1.1
            */
    }
    //1.2解析消息头
    private void parseHanders() throws IOException {
        Map<String,String> headers=new HashMap<>();
        while (true) {
            String line = readline();
            if (line.isEmpty()){
                break;
            }
            //headers.put(line.substring(0,line.indexOf(':')),line.substring(line.indexOf(':')+2,line.length()));
            String[] data = line.split(":\\s");
            headers.put(data[0],data[1]);

        }
        System.out.println("headers = " + headers);
        headers.forEach((k,v)->System.out.println(k+":"+v));
    }
    //1.3解析消息正文
    private void parseContent(){}



    private String readline() throws IOException {
        /*
            当socket对象是同一个时，无论调用多少次它的getInputStream（）方法获取的输入流始终是同一个输入流。
            输出流也是一样的
         */
        InputStream in = socket.getInputStream();
        int d;
        StringBuilder builder = new StringBuilder();
        char pre = 'a', cur = 'a';

        while ((d = in.read()) != -1) {
            cur = (char) d;
            if (pre == 13 && cur == 10) {
                break;
            }
            builder.append(cur);
            pre = cur;

        }
        return builder.toString().trim();

    }

    public String getMethod() {
        return method;
    }

    public String getUri() {
        return uri;
    }

    public String getProtocol() {
        return protocol;
    }

    public String getHeader(String name){
        return headers.get(name);
    }
}
